#ifndef _MY_HOT__ 
#define _MY_HOT__
#include "data.hpp"
#include "util.hpp"
#include <unistd.h>
extern cloud::DataManager *_data; //全局变量


namespace cloud
{
  class HotManager
  {
    private:
      time_t _hot_time = 30;
      std::string _backup_dir = "./backup_dir/";//要检测的源文件的存储路径

    
    public:
      HotManager()
      {
        FileUtil(_backup_dir).CreateDirectory();
      }




      bool IsHot(const std::string &filename)
      {
        time_t atime = FileUtil(filename).ATime();
        time_t ctime = time(NULL);//无符号64位数据
        if((ctime-atime) > _hot_time)
        {
          return false;
        }
        return true;
      }
      //1.遍历目录
      //2.遍历信息
      //3.获取指定文件时间属性，以当前系统时间，进行热点判断
      //4.非热点文件进行压缩
      //5.压缩完毕后，修改备份信息
    
      
      bool RunModule()
      {
        while(1){
          std::vector<std::string> array;
          FileUtil(_backup_dir).ScanDirectory(&array); // 1 访问这个目录下的文件名

          for (auto &file:array) //2
          {
            if(IsHot(file)==true) //3
            {
              continue;
            }
            else
            {
              std::cout<<file<<" is not hotfile!\n"<<std::endl;
            }
            FileInfo info;
            bool ret = _data->SelectOneByRealPath(file ,&info);

            if(ret == false)
            {
              //当前检测到的文件，没有历史备份信息
              //std::cout<<"an exception file was detected! delete it!\n"<<std::endl;
              //FileUtil(file).Remove();
              //continue;
              _data->Insert(file);
              _data->SelectOneByRealPath(file ,&info);
              std::cout<<info.pack_path<<std::endl;
            }
            else{
              std::cout<<info.pack_path<<std::endl;
            }
            FileUtil(file).Compress(info.pack_path); //4
            info.pack_flag = true;
            _data->UpdateStatus(file,true);//5  覆盖式处理，重新赋值
          }
          usleep(1000);//避免空目录情况下，空遍历消耗cpu资源过高
      
        }
        return true;
      }
  };
}



#endif
